package cn.fl.appeal.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.thirdauth.facade.auth.dto.ApiAppealRespDTO;
import cmm.thirdauth.facade.auth.dto.ApiApplyListQueryReqDTO;
import cmm.thirdauth.facade.auth.dto.ApiApplyListRespDTO;
import cmm.thirdauth.facade.base.dto.ApiThirdAuthPageReq;
import cmm.thirdauth.facade.base.dto.ApiThirdAuthPageResp;
import cmm.thirdauth.facade.base.dto.ApiThirdAuthReqDTO;
import cmm.thirdauth.facade.base.dto.ApiThirdAuthReqHeadDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.appeal.req.AppealReqDTO;
import cn.fl.appeal.service.LiveAppealService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 活体申诉 控制层
 */

@Controller
@RequestMapping("/liveappeal/liveappeal")
public class LiveAppealController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(LiveAppealController.class);

    @Autowired
    private LiveAppealService liveAppealService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        ModelAndView view = new ModelAndView("/liveappeal/liveappeal/liveAppealList");
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String certNo = request.getParameter("certNo");
        String usrNm = request.getParameter("usrNm");
        String flowStatus = request.getParameter("flowStatus");
        String status = request.getParameter("status");

        ApiThirdAuthReqDTO<ApiThirdAuthPageReq<ApiApplyListQueryReqDTO>> authReqDTO = new ApiThirdAuthReqDTO<>();
        ApiApplyListQueryReqDTO apiApplyListQueryReqDTO = new ApiApplyListQueryReqDTO();
        if (StringUtils.isNotEmpty(certNo)) {
            apiApplyListQueryReqDTO.setCertNo(certNo);
        }
        if (StringUtils.isNotEmpty(usrNm)) {
            apiApplyListQueryReqDTO.setUsrNm(usrNm);
        }
        if (StringUtils.isNotEmpty(flowStatus)) {
            apiApplyListQueryReqDTO.setFlowStatus(Short.parseShort(flowStatus));
        }
        if (StringUtils.isNotEmpty(status)) {
            apiApplyListQueryReqDTO.setStatus(Short.valueOf(status));
        }

        ApiThirdAuthPageReq authPageReq = new ApiThirdAuthPageReq();
        authPageReq.setQuery(apiApplyListQueryReqDTO);
        authPageReq.setPageSize(Integer.valueOf(pageSize));
        authPageReq.setPageNumber(Integer.valueOf(currentPage));
        authReqDTO.setBody(authPageReq);
        ApiThirdAuthReqHeadDTO apiThirdAuthReqHeadDTO = new ApiThirdAuthReqHeadDTO();
        apiThirdAuthReqHeadDTO.setpId("SQZS82JDKS999DDE");
        apiThirdAuthReqHeadDTO.setbId("5620526c50544796a414013337900ba4");
        authReqDTO.setHead(apiThirdAuthReqHeadDTO);

        ApiThirdAuthPageResp apiThirdAuthPageResp = liveAppealService.searchListPage(authReqDTO);
        ListPageVO<ApiApplyListRespDTO> listPage = new ListPageVO<ApiApplyListRespDTO>(apiThirdAuthPageResp.getRows(), Integer.valueOf(apiThirdAuthPageResp.getTotal().toString()));
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }

    /**
     * 到申诉详情页面
     */
    @RequestMapping(value = "/toAppealDetail.do", method = { RequestMethod.GET})
    public ModelAndView toAppealDetailPage(Long appealId) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAppealDetailPage", "加载申诉详情页面.请求参数：" + appealId, CurrentThreadContext.getCurrentUserName());
        ApiAppealRespDTO apiAppealRespDTO = liveAppealService.queryAppealDetail(appealId);
        ModelAndView view = new ModelAndView("/liveappeal/liveappeal/AppealDetail");
        view.addObject("detail", apiAppealRespDTO);
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "toAppealDetailPage", "加载申诉详情页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object update(AppealReqDTO appealReqDTO) throws BaseException {

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        if(StringUtils.isEmpty(appealReqDTO.getAppealId())){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("appealId不能为空");
            return result;
        }
        boolean b = liveAppealService.updateAppealInfo(appealReqDTO.getAppealId(), appealReqDTO.getBusRem());
        if (!b) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("保存审批信息失败");
            return result;
        }
        return result;
    }

    @RequestMapping(value = "/updateFileStatus.do", method = { RequestMethod.GET })
    @ResponseBody
    public Object updateFileStatus(String fileId, String status) throws BaseException {

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        if(StringUtils.isEmpty(fileId)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("文件id不能为空");
            return result;
        }
        if(StringUtils.isEmpty(status)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("文件状态不能为空");
            return result;
        }
        boolean b = liveAppealService.updateFileStatus(status, fileId);
        if (!b) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("保存资料状态失败");
            return result;
        }
        return result;
    }
}
